package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BlogMapper;
import com.itheima.pojo.*;
import com.itheima.service.TagService;
import com.itheima.utils.IdWorker;
import com.itheima.dao.TagMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class TagServiceImpl implements TagService {


    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BlogMapper blogMapper;

    //1 增加标签
    @Override
    public void add(Tag tag) {
        tag.setUid(idWorker.nextId()+"");
        tag.setStatus(1);
        tagMapper.insert(tag);
    }

    //2 批量删除标签
    @Override
    public Result deleteBatch(List<TagVO> list) {

        for (TagVO tagVO : list) {

            String uid = tagVO.getUid();
            Blog blog = new Blog();
            blog.setTagUid(uid);
            Blog blog1 = blogMapper.selectOne(blog);
            if (blog1!=null){
                return new Result(StatusCode.ERROR, "该标签下还有博客");
            }
            tagMapper.deleteByPrimaryKey(uid);
        }
        return new Result(StatusCode.OK, "删除成功");
    }

    //3 编辑标签
    @Override
    public void edit(Tag tag) {
     tagMapper.updateByPrimaryKey(tag);
    }

    //4 获取标签列表
    @Override
    public Page<Tag> getList(TagVO tagVO) {
        PageHelper.startPage(tagVO.getCurrentPage(),tagVO.getPageSize());
        Example example = createExample(tagVO);

        return (Page<Tag>)tagMapper.selectByExample(example);
    }

    //5  置顶标签
    @Override
    public void stick(TagVO tagVO) {

       //查询数据，封装数据
        int maxSort = tagMapper.findMaxSort();
        String uid = tagVO.getUid();
        Tag tag2 = new Tag();
        tag2.setUid(uid);
        Tag tag1 = tagMapper.selectOne(tag2);

        //设置当前博客分类的sort值比最大值多1
        Tag tag = new Tag();
        tag.setSort(maxSort+1);
        tag.setUid(tagVO.getUid());
        tag.setStatus(1);
        tag.setContent(tag1.getContent());
        tag.setClickCount(tag1.getClickCount());

        tagMapper.updateByPrimaryKey(tag);

    }

    //6 通过引用量排序标签
    @Override
    public void tagSortByCite() {
        int i = tagMapper.tagSortByCite();
        if (i>0){
            System.out.println("操作成功！");
        }
    }

    //7 通过点击量排序标签
    @Override
    public void tagSortByClickCount() {
        int i = tagMapper.tagSortByClickCount();
        if (i>0){
            System.out.println("操作成功！");
        }
    }


    //8. 获取标签的信息
    @Override
    public List<Tag> getTagList() {
        return tagMapper.getTagList();
    }


    /**
     * 通过uid查询tag对象
     * @param uid
     * @return
     */
    @Override
    public Tag getByUid(String uid) {
        return tagMapper.getByUid(uid);
    }

    /**
     * 根据点击数查询前5名
     * @return
     */
    @Override
    public List<Tag> findHotTag() {
        return tagMapper.findHotTag();
    }


    /*
     * 封装查询条件对象*/
    private Example createExample(TagVO tagVO) {
        Example example = new Example(BlogSort.class);
        example.setOrderByClause("sort DESC");
        Example.Criteria criteria = example.createCriteria();
        if (tagVO!= null) {
            // 根据标签名称模糊查询
            if(tagVO.getKeyword()!=null && !"".equals(tagVO.getKeyword())){
                criteria.andLike("content","%"+tagVO.getKeyword()+"%");
            }
        }
        return example;
    }
}
